#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>




#define PLUGIN_NAME                   "l4d2_tank_hula_hoop"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "给坦克添加粒子特效(呼啦圈)"   //介绍
#define PLUGIN_VERSION                "1.0.0"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站

public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}

#define CVAR_FLAGS			FCVAR_NOTIFY



int g_iBeamSprite, g_iHaloSprite, g_iCvarFieldColor[4];


bool g_bCvarFieldColorRandomapp, g_bCvarFieldBeaconapp;
char g_sCvarFieldColor[12];

ConVar g_hCvarFieldBeaconapp, g_hCvarFieldColorapp, g_hCvarFieldStartRadiusapp, g_hCvarFieldEndRadiusapp, g_hCvarFieldDurationapp, g_hCvarFieldWidthapp, g_hCvarFieldAmplitudeapp, g_TimerHealingapp;

float g_fCvarFieldStartRadiusapp, g_fCvarFieldEndRadiusapp, g_fCvarFieldDurationapp, g_fCvarFieldWidthapp, g_fCvarFieldAmplitudeapp, g_TimerHealing;

#define SPRITE_BEAM			"materials/sprites/laserbeam.vmt"
#define SPRITE_HALO			"materials/sprites/glow01.vmt"


public void OnPluginStart()
{
	HookEvent("tank_spawn", Event_Tank_Spawn, EventHookMode_Pre);   //坦克出生事件.
	
	g_hCvarFieldBeaconapp =			    CreateConVar(	"l4d2_hoop_healing_field_heal_beacon",			"1",			"0=关闭。1=生成一个信标.", CVAR_FLAGS, true, 0.0, true, 1.0 );
	g_hCvarFieldColorapp =				CreateConVar(	"l4d2_hoop_healing_field_color",				"0 255 0",		"呼啦圈领域的颜色。0-255 之间的三个值，以空格分隔.", CVAR_FLAGS );
	g_hCvarFieldStartRadiusapp =		CreateConVar(	"l4d2_hoop_healing_field_start_radius",		    "0.0",		    "呼啦圈起始半径.", CVAR_FLAGS, true, 0.0 );
	g_hCvarFieldEndRadiusapp =			CreateConVar(	"l4d2_hoop_healing_field_end_radius",			"120.0",		"呼啦圈区域末端半径.", CVAR_FLAGS, true, 0.0 );
	g_hCvarFieldDurationapp =			CreateConVar(	"l4d2_hoop_healing_field_duration",			    "1.0",			"呼啦圈应该持续多少秒.", CVAR_FLAGS, true, 0.0 );
	g_TimerHealingapp =			        CreateConVar(	"l4d2_hoop_g_TimerHealingapp",			        "1.0",			"呼啦圈应该多少秒后触发.", CVAR_FLAGS, true, 0.0 );
	g_hCvarFieldWidthapp =				CreateConVar(	"l4d2_hoop_healing_field_width",				"3.0",			"呼啦圈区域宽度.", CVAR_FLAGS, true, 0.0 );
	g_hCvarFieldAmplitudeapp =			CreateConVar(	"l4d2_hoop_healing_field_amplitude",			"0.0",			"呼啦圈振幅.", CVAR_FLAGS, true, 0.0 );
	CreateConVar(									"l4d2_tank_hula_hoop_version",							PLUGIN_VERSION,	    "插件版本.", FCVAR_NOTIFY);
	//AutoExecConfig(true,							"l4d2_tank_hula_hoop");
	
	

	HookConVarChange(g_hCvarFieldBeaconapp, eConvarChanged);
	HookConVarChange(g_hCvarFieldColorapp, eConvarChanged);
	HookConVarChange(g_hCvarFieldStartRadiusapp, eConvarChanged);
	HookConVarChange(g_hCvarFieldEndRadiusapp, eConvarChanged);
	HookConVarChange(g_hCvarFieldDurationapp, eConvarChanged);
	HookConVarChange(g_hCvarFieldWidthapp, eConvarChanged);
	HookConVarChange(g_hCvarFieldAmplitudeapp, eConvarChanged);
	HookConVarChange(g_TimerHealingapp, eConvarChanged);
}

public void Event_Tank_Spawn(Event event, const char[] name, bool dontBroadcast)
{
	int userid = GetEventInt(event, "userid");
	int Client = GetClientOfUserId(userid);
	if (IsInfected(Client))
	{
		CreateTimer(g_TimerHealing, TimerHealingField, userid, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
	}	
}

bool IsInfected(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 3;
}

bool IsTank(int client)  
{
	if (!IsInfected(client))
		return false;

	if (GetEntProp(client, Prop_Send, "m_zombieClass") != 8)
		return false;

	return true;
}

public void OnMapStart()
{
	g_iBeamSprite = PrecacheModel(SPRITE_BEAM, true);
	g_iHaloSprite = PrecacheModel(SPRITE_HALO, true);

	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{	
	g_hCvarFieldColorapp.GetString(g_sCvarFieldColor, sizeof(g_sCvarFieldColor));
	g_bCvarFieldColorRandomapp = StrEqual(g_sCvarFieldColor, "random");
	g_iCvarFieldColor = GetColors(g_sCvarFieldColor);
	g_bCvarFieldBeaconapp = g_hCvarFieldBeaconapp.BoolValue;
	g_fCvarFieldStartRadiusapp = g_hCvarFieldStartRadiusapp.FloatValue;
	g_fCvarFieldEndRadiusapp = g_hCvarFieldEndRadiusapp.FloatValue;
	g_fCvarFieldDurationapp = g_hCvarFieldDurationapp.FloatValue;
	g_fCvarFieldWidthapp = g_hCvarFieldWidthapp.FloatValue;
	g_fCvarFieldAmplitudeapp = g_hCvarFieldAmplitudeapp.FloatValue;
	g_TimerHealing = g_TimerHealingapp.FloatValue;	
}

public Action TimerHealingField(Handle timer, any client)
{
	client = GetClientOfUserId(client);
	if(IsTank(client) && IsPlayerAlive(client))
	{

		float vpos[3];
		float entpos[3];
		

		GetEntPropVector(client, Prop_Send, "m_vecOrigin", vpos);
		for (int i = 1; i <= MaxClients; i++)
		{
			if (!IsSurvivor(i) || !IsPlayerAlive(i))
				continue;
			GetEntPropVector(i, Prop_Send, "m_vecOrigin", entpos);
			
			if (g_bCvarFieldBeaconapp)
			{
				CreateBeamRing(vpos);
			}
		}
	}
	return Plugin_Continue;
}

bool IsSurvivor(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2;
}

void CreateBeamRing(float vPos[3])
{
	int colors[4];

	if( g_bCvarFieldColorRandomapp )
		colors = GetRandomColors();
	else
		colors = g_iCvarFieldColor;

	vPos[2] += 0.0;
	//(目标, 初始半径, 最终半径, 效果1, 效果2, 渲染贴, 渲染速率, 持续时间, 播放宽度,播放振幅, 顏色(Color[4]), (播放速度)10, (标识)0)
	TE_SetupBeamRingPoint(vPos, g_fCvarFieldStartRadiusapp, g_fCvarFieldEndRadiusapp, g_iBeamSprite, g_iHaloSprite, 0, 0, g_fCvarFieldDurationapp, g_fCvarFieldWidthapp, g_fCvarFieldAmplitudeapp, colors, 0, 0 );
	TE_SendToAll();
}

int[] GetRandomColors()
{
	int colors[4];

	colors[0] = GetRandomInt(0, 255);
	colors[1] = GetRandomInt(0, 255);
	colors[2] = GetRandomInt(0, 255);
	colors[3] = 255;

	return colors;
}

int[] GetColors(char[] sColor)
{
	int colors[4];

	if( sColor[0] == 0 )
		return colors;

	char sColors[3][4];
	int color = ExplodeString(sColor, " ", sColors, sizeof(sColors), sizeof(sColors[]));

	if( color != 3 )
		return colors;

	colors[0] = StringToInt(sColors[0]);
	colors[1] = StringToInt(sColors[1]);
	colors[2] = StringToInt(sColors[2]);
	colors[3] = 255;

	return colors;
}














